/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"09FACD4C-FDCE-4579-B263-196BD3FE3D3E"}
 */
var fPass = null;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"6DD80AF2-4F20-4461-9FBD-EFDDBD649FCE"}
 */
function onShow(firstShow, event) {
	controller.loadRecords(globals.gUservID)
	databaseManager.setAutoSave(false)
	elements.logo.imageURL = url_logo
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"335B83EB-CCE4-4D06-AF8A-BA80C480FA6C"}
 */
function guardar(event) {
	
	if (fPass)
		mail_pass = globals.cryptString(fPass, globals.cryptKey, 'C')
	
	if (databaseManager.saveData())
		globals.DIALOGS.showInfoDialog('Aviso','Los cambios fueron guardados','OK')
	else
		globals.DIALOGS.showWarningDialog('Aviso','Ocurrió un error al grabar los cambios','OK')
}

/**
 * Handle hide window.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"5B4958DB-13EF-4C0A-A537-49AE5330EFBD"}
 */
function onHide(event) {
	databaseManager.revertEditedRecords()
	databaseManager.setAutoSave(true)
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"0EEE5F90-E946-4323-AC2D-A8E2B186B538"}
 */
function buscarLogo(event) {
	plugins.file.showFileOpenDialog(null, null, false, new Array("Images", "jpg", "gif", "png", "*"), grabarLogo, 'Seleccione el logo')
}

/**
 * @param {Array<plugins.file.JSFile>} foto
 *
 * @properties={typeid:24,uuid:"22EC869A-7DC6-4A4D-AFAF-7812DD615257"}
 */
function grabarLogo(foto)
{
	var $arch 	= foto[0]
	var $nombre = $arch.getName()
	
	if ($arch.getContentType().indexOf('image') == -1)
	{
		globals.DIALOGS.showWarningDialog('Aviso','El archivo debe ser una imagen','OK')
		return
	}
	
	var $img 	= plugins.images.getImage($arch.getBytes())
	
	var $dir 	= plugins.file.getDefaultUploadLocation()+ '/fac_electronica/'+ userv_id
	var $folder = plugins.file.createFile($dir)
	var f 		= plugins.file.convertToJSFile($folder);
	if (!f.exists())
		plugins.file.createFolder($dir)

	if ($img.getHeight() > 65 || $img.getWidth() > 100)
		$img = $img.resize(100,65)
	var $file = plugins.file.createFile($dir+'/'+$nombre)
	
	var $data = $img.getData()//$arch.getBytes();
	if (plugins.file.writeFile($file, $data, $arch.getContentType()))
	{
		url_logo = application.getServerURL()+'/uploads/fac_electronica/'+ userv_id+ '/' + $nombre
		elements.logo.imageURL = url_logo
	}
	else
		globals.DIALOGS.showWarningDialog('Aviso','No se pudo subir la imagen','OK')
}
